#ifndef __I2C_H__
#define __I2C_H__

#include "gd32f30x.h"

#define I2C1_MODE_POLL              0x00
#define I2C1_MODE_INT               0x01
#define I2C1_MODE_DMA               0x02
#define I2C1_MODE_SLAVE             0x03
#define I2C1_MODE_SW                0x04


#define I2C1_MODE                   I2C1_MODE_SW

#define I2C1_SLAVE_ADDR             0xA0
#define I2C1_SPEED                  100000

#if I2C1_MODE == I2C1_MODE_POLL
void i2c1_config(void);
void i2c1_write_data_by_reg(uint8_t slave_addr,uint8_t* p_buffer, uint8_t write_address, uint8_t number_of_byte);
void i2c1_read_data_by_reg(uint8_t slave_addr,uint8_t* p_buffer, uint8_t read_address, uint16_t number_of_byte);

#elif I2C1_MODE == I2C1_MODE_DMA
#define ARRAYNUM(arr_nanme)      (uint32_t)(sizeof(arr_nanme) / sizeof(*(arr_nanme)))
//#define I2C0_DATA_ADDRESS        0x40005410
#define I2C1_DATA_ADDRESS        0x40005810
#define I2C_TX_RX_DATA_LEN_MAX   128

void i2c1_dma_send_data(uint8_t slave_addr,uint8_t *reg_addr,uint16_t addr_len,uint8_t *p_data,uint8_t data_len);
void i2c1_dma_init();
void i2c1_dma_receive_data(uint8_t slave_addr, uint8_t *reg_addr,uint16_t addr_len,  
            uint8_t* p_buffer, uint16_t number_of_byte);

#elif I2C1_MODE == I2C1_MODE_SLAVE
#define I2C1_OWN_ADDRESS7      0x30

extern volatile uint8_t        i2c_txbuffer[128];
extern volatile uint8_t        i2c_rxbuffer[128];

void i2c1_slave_config(void);

#elif I2C1_MODE == I2C1_MODE_SW

void swi2c_config(void);
void swi2c_search_device_addr(void);

//针对eeprom使用
void swi2c_device_write_bytes(uint8_t device_addr,uint8_t reg_addr,uint8_t *buf,uint8_t len);
void swi2c_device_read_bytes(uint8_t device_addr,uint8_t reg_addr,uint8_t *buf,uint8_t len);

void swi2c_device_read_data(uint8_t device_addr,uint8_t *reg_addr,
                                    uint16_t reg_len,uint8_t *buf,uint8_t len);
void swi2c_device_write_data(uint8_t device_addr,uint8_t *reg_addr,
                                    uint16_t reg_len,uint8_t *buf,uint8_t len);
void swi2c_read_data(uint8_t device_addr,uint8_t *buf,uint8_t len);
void swi2c_write_data(uint8_t device_addr,uint8_t *buf,uint8_t len);
#endif // I2C1_MODE == I2C1_MODE_POLL

#endif //__I2C_H__